package com.example.myandroiduseflutter

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.example.myandroiduseflutter.databinding.FragmentAndroidMainBinding
import com.idlefish.flutterboost.containers.FlutterBoostActivity
import io.flutter.embedding.android.FlutterActivityLaunchConfigs
import java.util.*

/**
 * @author tgw
 * @date 2021/10/8
 * @describe
 */
class AndroidFragment : Fragment() {

    companion object {

        private const val REQUEST_CODE = 1000
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val binding: FragmentAndroidMainBinding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_android_main, container, false)

        binding.btFlutter.setOnClickListener() {
            //开启一个flutter界面
            val params = HashMap<String,Any>()
            params["tgw1"] = "tgw1"
            params["tgw2"] = 123
            val intent =
                FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity::class.java)
                    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.opaque)
                    .destroyEngineWithActivity(false)
                    .url("SimpleWidget")
                    .urlParams(params)
                    .build(context)
            startActivityForResult(intent, REQUEST_CODE)
        }

        return binding.root
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.v(
            "tgw log",
            "#onActivityResulttgw, requestCode=" + requestCode + ", resultCode=" + resultCode + ", data=" + data!!.getSerializableExtra(
                com.idlefish.flutterboost.containers.FlutterActivityLaunchConfigs.ACTIVITY_RESULT_KEY
            )
        )

    }
}